package com.pai4j.search.service.es;

import com.pai4j.common.util.JsonUtil;
import com.pai4j.domain.vo.response.moment.MomentVO;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * π圈动态 ES 服务
 */
@Slf4j
@Service
public class MomentElasticSearchService extends AbstractPai4jElasticSearchClient<MomentVO> {

    @Value("${spring.elasticsearch.moment-index:pai4j_moment_dev}")
    private String momentIndex;

    @Override
    protected String getIndexName() {
        return momentIndex;
    }

    @Override
    protected XContentBuilder getXContentBuilder() throws IOException {
        return JsonXContent.contentBuilder()
                .startObject()
                .startObject("properties")
                .startObject("type").field("type", "integer").endObject()
                .startObject("biz_id").field("type", "keyword").endObject()
                .startObject("content")
                    .field("type", "text")
                    .field("analyzer", "ik_smart")
                    .field("search_analyzer", "ik_smart")
                    .startObject("fields")
                        .startObject("raw").field("type", "keyword").field("ignore_above", 256).endObject()
                    .endObject()
                .endObject()
                .startObject("create_date").field("type", "date").field("format", "yyyy-MM-dd").endObject()
                .startObject("update_date").field("type", "date").field("format", "yyyy-MM-dd").endObject()
                .endObject()
                .endObject();
    }

    @Override
    protected MomentVO convert(String dataJson) {
        return JsonUtil.fromJson(dataJson, MomentVO.class);
    }

    @Override
    protected void setSearchResultExtInfo(List<MomentVO> dataList) {
        // 暂无扩展
    }
}
